<?php
$g4_path = "../"; 
$title = "교재 카테고리 관리";
$menu = 1;
require_once ($g4_path.'common.php');
require_once ('./admin.head.php');
require_once ('./admin.lib.php');
?>
<h1><?php echo $title; ?></h1>
<style>
    div.containerDiv {
        padding: 10px;
        border:1px gray solid;
    }
    
    div.containerDiv ul li {
        margin-left:20px;
    }

</style>
<script>
    function addCategory(depth, node) {
        
        var nodeName = node.parentNode.getAttribute('value');
        var addCategoryName = prompt(nodeName + " 하위에 새로 추가할 카테고리 이름은?", "예) 영어");
        var newElement = document.createElement('li'); 
        newElement.innerHTML = addCategoryName;
        var targetNode = node.parentNode;
   
    // todo : 동일 depth 안에 동중복 명칭이 있을때를 대비하여 parent의 parent도 받아오기 
/*        new AjaxRequest('./bookCategoryProc.php', { 
            method: "post",
            encoding: "utf-8'",
            parameters: array('method'=>'add','depth'=>depth,'parent'=>nodeName),
            onSuccess : function(trans) {
                var temp = trans.responseText;
                alert(temp);
            }
       });
   */ 
        targetNode.appendChild(newElement); 
    }
    
    function delCategory(depth, node, id) {
        var confirmCheck = confirm("정말 삭제하시겠습니까?\n삭제된 데이터는 모두 백업으로 이동됩니다.");
        
        // todo:: 해당 category 모두 backup으로 보내기
        if(confirmCheck) {
            var targetId = document.getElementById(id);
            node.parentNode.parentNode.removeChild(targetId);
        }
    }
</script>
<?php

function prnFuncList($depth = '1', $_id) {
    if($depth == "1" || $depth =="2") {
        echo ' <a onClick="javascript:addCategory(\''.$depth.'\',this)" href="#">+</a> <a onClick="javascript:delCategory(\''.$depth.'\',this, \''.$_id.'\')" href="#">x</a>';
    } else {
        echo ' <a onClick="javascript:delCategory(\''.$depth.'\',this, \''.$_id.'\')" href="#">x</a>';
    }
}


$_cate1Query = "SELECT cate1 as v, count(cate2) as s FROM book_category WHERE `cate1` != '백업' GROUP BY cate1";
$_cate1 = sql_query($_cate1Query);
echo '<div class="containerDiv"><ul>';
$num = 0;


//todo :: recursive 반영하기
while($list1 = sql_fetch_array($_cate1)) { //depth1 

    echo '<li id="li'.$num.'" value="'.$list1['v'].'"><span>'.$list1['v'].'</span>';
    echo prnFuncList('1', 'li'.$num);
    $num++; 
    if($list1['s'] < 1) {
        echo '</li>';
    } else {
        echo '<ul>';    
        $_cate2Query="SELECT cate2 as v, count(cate3) as s FROM book_category WHERE cate1 = '".$list1['v']."' GROUP BY cate2"; 
        $_cate2 = sql_query($_cate2Query);
        while($list2 = sql_fetch_array($_cate2)) { //depth2
            if($list2['v']) {
            echo '<li id="li'.$num.'" value="'.$list2['v'].'"><span>'.$list2['v'].'</span>';
            echo prnFuncList('2', 'li'.$num);
            $num++; 
            }
            if($list2['s'] < 1) {
                echo '</li>';
            } else {
                echo '<ul>';    
                $_cate3Query="SELECT cate3 as v FROM book_category WHERE cate1 = '".$list1['v']."' AND cate2 = '".$list2['v']."' GROUP BY cate3";
                $_cate3 = sql_query($_cate3Query);
                while($list3 = sql_fetch_array($_cate3)) { //depth3 
                if($list3['v']) {
                    echo '<li id="li'.$num.'" value="'.$list3['v'].'"><span>'.$list3['v'].'</span>';
                    echo prnFuncList('3', 'li'.$num);
                    echo '</li>';
                    $num++; 
                }
                }

                echo '</ul></li>';
            }
        }
        echo '</ul></li>';
    }
}
echo '<li id="backup">백업</li>';
echo '</ul></div>';
